
/**
 * 广告支出-按行业属性统计
 */  
$(function(){
	//跳转输入值正则验证
	function inputpattern(){
		var inputVal=$("input[name='selectPage']").val();
		var patInput=/^[1-9]\d*$/;   //正整数
		if(patInput.test(inputVal)){
			return true;
		}else{
			return false;
		}
	}
	$(".pageBox button").css("background-color","#478df1");
	$(".pageBox button").on("click",function(){
		var result=inputpattern();
		var showPageN=$(".pagination").children().length;
		var activeLi=$(".active a").html();
		page=$("input[name='selectPage']").val();
		if(result){
			if(page==activeLi){
				
			}else{
				if(page==1){
					var byindustry = $("#byindustry").val();
					byTotalIndustryDay(page,byindustry);
				}else if(page>showPageN-4){
					
				}else{
					byTotalIndustry0(page);
				}
			}
		}
		$("input[name='selectPage']").val("");
	});
	
});
function byTotalIndustry0(page){
	if(page==null || page.length<=0){
		var page=1;
	}
	var startTime;
	var endTime;
	var byindustry=$("#byindustry").val();
		$.ajax({
			url : '/web-adv/byTotalIndustry',
	        data:{"startTime":startTime,"endTime":endTime,"page":page,"byindustry":byindustry},
			type : 'post',
			dataType:'json',
			success : function(data) {
				$("#endTime").val(data.endTime);
				$("#startTime").val(data.startTime);	
				$('#pageBox').extendPagination({
					totalCount:data.total,
			 		showPage: data.pages+1,
		            limit: data.pageSize,
		            goPage:page,
		            callback: function (page) {
		            	byTotalIndustry2(page);	
		            }
				});
			},
	        error: function(){}        
	});
};

function byTotalIndustry(page){
	if(page==null || page.length<=0){
		var page=1;
	}
	var startTime;
	var endTime;
		$.ajax({
			url : '/web-adv/byTotalIndustry',
	        data:{"startTime":startTime,"endTime":endTime,"page":page},
			type : 'post',
			dataType:'json',
			success : function(data) {
				$("#endTime").val(data.endTime);
				$("#startTime").val(data.startTime);	
				var beans = data.beans;
				var proportions=0;
				var recharges=0;
				var adBudgets=0;
				var MyPays=0;
				$('#pageBox').extendPagination({
					totalCount:data.total,
			 		showPage: data.pages+1,
		            limit: data.pageSize,
		            goPage:page,
		            callback: function (page) {
		            	byTotalIndustry2(page);	
		            }
				});
				//循环输出表格
				for(var i = 0; i < beans.length;i++){
					MyPays+=parseInt(beans[i].adPay);
					
					var tbody = "<tr><td>"+(i+1)+"</td>" +
					"<td>"+beans[i].adIndustry +"</td>" +
					"<td>"+beans[i].adCount+"</td>" +
					"<td>"+(beans[i].averagePrice*1).toFixed(2)+"</td>" +
					"<td>"+beans[i].clickNum+"</td>" +
					"<td>"+(beans[i].adPay*1).toFixed(2)+"</td>" +
					"<td>"+(beans[i].proportion*100).toFixed(2)+'%'+"</td>" +
					"<td>"+beans[i].adBudget+"</td>" +
					"<td>"+((page-1)*5+(i+1))+"</td>"+
					"<td><a class='btn btn-primary' href='/web-adv/advWeb/views/adTotalPay/totalByAdIndustryOneday.html?selectedNav=3&navPos=4&endTime="+data.endTime+"&startTime="+data.startTime+"&byindustry="+beans[i].adIndustry+"'>查看</a></td>"+"</tr>"
					
					$('#totalByAdIndustryOneday').append(tbody);
					proportions+=beans[i].proportion*100;
					adBudgets+=beans[i].adBudget*1;
					clickNums=beans[i].clickNum*1;
				}
				var tbodyTwo="<tr>" +
				"<td>总计</td>" +
				"<td>---</td>" +
				"<td>---</td>" +
				"<td>---</td>" +
				"<td>"+clickNums+"</td>" +
				"<td>"+MyPays.toFixed(2)+"</td>" +
			    "<td>"+proportions.toFixed(2)+'%'+"</td>" +
			    "<td>"+adBudgets.toFixed(2)+"</td>" +
			    "<td>---</td>" +
			    "<td>---</td>" +
			 "</tr>";
					$('#totalByAdIndustryOneday').append(tbodyTwo);
			},
	        error: function(){}        
		});
  };





function byTotalIndustry2(page){
	var startTime;
	var endTime;
	var byindustry=$("#byindustry").val();
		$.ajax({
			url : '/web-adv/byTotalIndustry',
	        data:{"startTime":startTime,"endTime":endTime,"page":page,"byindustry":byindustry},
			type : 'post',
			dataType:'json',
			success : function(data) {
				$('#totalByAdIndustryOneday').find("tr").eq(0).siblings("tr").remove();
				$("#endTime").val(data.endTime);
				$("#startTime").val(data.startTime);	
				var beans = data.beans;
				var proportions=0;
				var recharges=0;
				var adBudgets=0;
				var MyPays=0;
				//循环输出表格
				for(var i = 0; i < beans.length;i++){
					//总支出
					MyPays+=parseInt(beans[i].adPay);
					var tbody = "<tr><td>"+(i+1)+"</td>" +
					"<td>"+beans[i].adIndustry +"</td>" +
					"<td>"+beans[i].adCount+"</td>" +
					"<td>"+(beans[i].averagePrice*1).toFixed(2)+"</td>" +
					"<td>"+beans[i].clickNum+"</td>" +
					"<td>"+(beans[i].adPay*1).toFixed(2)+"</td>" +
					"<td>"+(beans[i].proportion*100).toFixed(2)+'%'+"</td>" +
					"<td>"+beans[i].adBudget+"</td>" +
					"<td>"+((page-1)*5+(i+1))+"</td>"+
					"<td><a class='btn btn-primary' href='/web-adv/advWeb/views/adTotalPay/totalByAdIndustryOneday.html?selectedNav=3&navPos=4&endTime="+data.endTime+"&startTime="+data.startTime+"&byindustry="+beans[i].adIndustry+"'>查看</a></td>"+"</tr>"
					
					$('#totalByAdIndustryOneday').append(tbody);
					proportions+=parseInt(beans[i].proportion);
					adBudgets+=beans[i].adBudget*1;
					clickNums=beans[i].clickNum*1;
				}
				var tbodyTwo="<tr>" +
				"<td>总计</td>" +
				"<td>---</td>" +
				"<td>---</td>" +
				"<td>---</td>" +
				"<td>"+clickNums+"</td>" +
				"<td>"+MyPays.toFixed(2)+"</td>" +
			    "<td>"+proportions.toFixed(2)+'%'+"</td>" +
			    "<td>"+adBudgets.toFixed(2)+"</td>" +
			    "<td>---</td>" +
			    "<td>---</td>" +
			 "</tr>";
					$('#totalByAdIndustryOneday').append(tbodyTwo);
			},
	        error: function(){}        
		});
}

function byTotalIndustryDay(startTime,endTime,byindustry){
	if(page==null || page.length<=0){
		var page=1;
	}
	
		$.ajax({
			url : '/web-adv/byTotalIndustry',
	        data:{"startTime":startTime,"endTime":endTime,"page":page,"byindustry":byindustry},
			type : 'post',
			dataType:'json',
			success : function(data) {
				$('#totalByAdIndustryOneday').find("tr").eq(0).siblings("tr").remove();
				$("#endTime").val(data.endTime);
				$("#startTime").val(data.startTime);	
				var beans = data.beans;
				var proportions=0;
				var recharges=0;
				var adBudgets=0;
				var MyPays=0;
				$('#pageBox').extendPagination({
					totalCount:data.total,
			 		showPage: data.pages+1,
		            limit: data.pageSize,
		            goPage:page,
		            callback: function (page) {
		            	byTotalIndustry2(page);	
		            }
				});
				//循环输出表格
				for(var i = 0; i < beans.length;i++){
					MyPays+=parseInt(beans[i].adPay);
					
					var tbody = "<tr><td>"+(i+1)+"</td>" +
					"<td>"+beans[i].adIndustry +"</td>" +
					"<td>"+beans[i].adCount+"</td>" +
					"<td>"+(beans[i].averagePrice*1).toFixed(2)+"</td>" +
					"<td>"+beans[i].clickNum+"</td>" +
					"<td>"+(beans[i].adPay*1).toFixed(2)+"</td>" +
					"<td>"+(beans[i].proportion*100).toFixed(2)+'%'+"</td>" +
					"<td>"+beans[i].adBudget+"</td>" +
					"<td>"+((page-1)*5+(i+1))+"</td>"+
					"<td><a class='btn btn-primary' href='/web-adv/advWeb/views/adTotalPay/totalByAdIndustryOneday.html?selectedNav=3&navPos=4&endTime="+data.endTime+"&startTime="+data.startTime+"&byindustry="+beans[i].adIndustry+"'>查看</a></td>"+"</tr>"
					
					$('#totalByAdIndustryOneday').append(tbody);
					proportions+=parseInt(beans[i].proportion);
					adBudgets+=beans[i].adBudget*1;
					clickNums=beans[i].clickNum*1;
				}
				var tbodyTwo="<tr>" +
				"<td>总计</td>" +
				"<td>---</td>" +
				"<td>---</td>" +
				"<td>---</td>" +
				"<td>"+clickNums+"</td>" +
				"<td>"+MyPays.toFixed(2)+"</td>" +
			    "<td>"+proportions.toFixed(2)+'%'+"</td>" +
			    "<td>"+adBudgets.toFixed(2)+"</td>" +
			    "<td>---</td>" +
			    "<td>---</td>" +
			 "</tr>";
					$('#totalByAdIndustryOneday').append(tbodyTwo);
			},
	        error: function(){}        
	});
}